home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13661 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.8 KB  |  173 lines

  1. Newsgroups: comp.lang.c++,comp.lang.java,comp.object,comp.software-eng
  2. Path: lego.wes.mot.com!mothost!schbbs!news
  3. From: shang@corp.mot.com (David L. Shang)
  4. Subject: Re: Pointers are hacks in c++ (portablity hackers etc)
  5. Reply-To: shang@corp.mot.com
  6. Organization: MOTOROLA 
  7. Date: Tue, 26 Mar 1996 14:46:25 GMT
  8. Message-ID: <1996Mar26.144625.2906@schbbs.mot.com>
  9. References: <4j76i9$pkt@news.roadnet.ups.com>
  10. Sender: news@schbbs.mot.com (SCHBBS News Account)
  11. Nntp-Posting-Host: 129.188.128.126
  12.  
  13. In article <4j76i9$pkt@news.roadnet.ups.com> mwm@roadnet.ups.com (Marlin Meier)  
  14. writes:
  15. > I have found a book that deals with c++ pointers and pointer fear
  16. > exclusivly.  464 pages about pointers and memory management!  This is
  17. > what I have been looking for.
  18.  
  19. 464 pages just about pointers!
  20.  
  21. In article <EJH.96Mar19163745@larry.gsfc.nasa.gov> ejh@larry.gsfc.nasa.gov  
  22. (Edward Hartnett) writes:
  23. > Hmmm. Most of what I would call hacked code is not code that violates
  24. > the standard by depending on undefined behaviour, but rather code that
  25. > does weird (but usually standard conforming) things with memory or
  26. > pointers.
  27.  
  28. Exactly!
  29.  
  30. In <vpwa1m6wu.fsf_-_@jlbaker.async.csuohio.edu>, 
  31. jabaker@grail.cba.csuohio.edu (jason) writes:
  32.  
  33. > What exactly constitutes pointer or memory weirdness?  I think that
  34. > going through a list like:
  35. >   list_cell **cell = &head;
  36. >   while (*cell && (*cell)->member != value) cell = &cell->next;
  37. > looks sort of weird, but is a well established practice.  XtOffset and
  38. > stl's list cell management both do weird things, but encapsulate the
  39. > gory details, and presumably work as intended.  Are these evil hacks?
  40.  
  41. Yes, they are.
  42.  
  43. A good, understandable program should not use things like *, &, ->
  44. to increase the complexicity of the code. A good programming
  45. language should not encourage users to abuse these weird operators
  46. in a way like **x and &x->y.
  47.  
  48. The member selection operator "." and the assignment ":=" are
  49. sufficient to handle all the regular things. Operator "*" and "&"
  50. should only be used for some particular situition in a low-level
  51. system programming, for example, the absolute address is required
  52. for some embedded system.
  53.  
  54. Many OO languages including Eiffel and Java have tried to eliminate
  55. the pointer arithmetic. Operators "*", "&" and "->" are no longer
  56. required.
  57.  
  58. But what happens if more than one new object references are required
  59. from a function interface? In C++ we can do:
  60.  
  61.     void foo (C**x, D** y) { *x =newC(); *y =newD(); };
  62.     C* x;
  63.     D* y;
  64.     foo(&x,&y);
  65.  
  66. How can we do in Java? Perhaps we have to construct a new class:
  67.  
  68.     class Output
  69.     {
  70.         C x;
  71.         D y;
  72.     };
  73.     class bar
  74.     {
  75.         void foo (Output out) { out.x =newC(); out.y=new D(); };
  76.         bar()
  77.         {
  78.             Output xy;
  79.             foo (xy);
  80.         }
  81.     };
  82.  
  83. It seems even more clumsy than C++'s code.
  84.  
  85. Transframe's interface allows output to be a list in the same way as
  86. we define an input:
  87.  
  88.     input_list_type: output_list_type
  89.  
  90. Therefore we can have:
  91.  
  92.     function foo (): (C, D) { return (C(), D()); };
  93.     x: C;
  94.     y: D;
  95.     (x, y) := foo();
  96.  
  97. or, we can write:
  98.  
  99.     xy: (C, D) = foo();
  100.  
  101. or, if we need to access member of "xy" via member names:
  102.  
  103.     xy: (x:C, y:D) = foo();
  104.  
  105.  
  106. Note that the "new" operator is eliminated also.
  107.  
  108. The concept of "name" covers every thing you can find in Java's
  109. smart reference, C++'s valued variable, or even the low-level
  110. pointers (if it is required in systems programming). But the
  111. concept of "name" goes far beyond the current limitation.
  112.  
  113. Names are themselves objects. Users can define their own names.
  114. But all names follow the same usage interface.
  115.  
  116. Some interesting user-defined names:
  117.  
  118. * net reference:
  119.  
  120.    x: URL#("http://www.foo.com/hello.tf") of TFApplet;
  121.    if (x) x.run();
  122.  
  123.    object my_World3D is World3D
  124.    {
  125.     x: URL#("http://www.foo.com/3d/global.3d") of Model3D;
  126.     enter()
  127.     {    if (!x) return;
  128.         i: float =0;
  129.         for (i=0; i<=1; i+=0.1)
  130.         {
  131.             with (x)
  132.             {
  133.             rotate (0, 0, i*360, LOCAL_COORDINATE);  
  134.             scale (i,i,i);
  135.             re_display();
  136.             }
  137.         }
  138.     }
  139.    }; 
  140.         
  141.    Note that the usage of "with" statement.
  142.  
  143. * team reference:
  144.  
  145.    dancing_team: TEAM of AnimatingObject;
  146.  
  147.    // a dancing team is created
  148.    dancing_team := (    AnimatingCow("cow"),
  149.             AnimatingDog("dog"),
  150.             AnimatingCat("cat")
  151.            );
  152.  
  153.    // team members are handled individually
  154.    foreach (member in dancing_team) member.move_to(somePosition);
  155.  
  156.    // now team members are called together by the team reference
  157.    // "dance" is a method of the "AnimatingObject" class
  158.    dancing_team.dance (anDancingScript);
  159.  
  160.    // now a particular team member is called
  161.    dancing_team.Member("cat").go_away();
  162.  
  163.    // now the team itself (not a member of the team) is called
  164.    // "WelcomeNewMember" is a method of the "TEAM" class
  165.    dancing_team.WelcomeNewMember(AnimatingSheep("sheep"));
  166.  
  167. For detail, wait for my column on the May issue of Object Currents
  168. (http://www.sigs.com/objectcurrents/).
  169.  
  170. David Shang
  171.  
  172.